package com.zyx.addressBook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

public class VCardManager {
	Activity activity;
	
	
	public VCardManager(Activity activity) {
		super();
		this.activity = activity;
	}


	public void doVCardsExport() throws IOException
	{
		ContentResolver cr = activity.getContentResolver();
		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
		String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
		Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
		AssetFileDescriptor fd = activity.getContentResolver().openAssetFileDescriptor(uri, "r");
		FileInputStream fis = fd.createInputStream();
		//CMFileManager.writeBufToFile("test.vcard", fis.toString().getBytes());
	}
	
	public String doVCardsExportToString() throws IOException
	{
		ContentResolver cr = activity.getContentResolver();
		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
		String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
		Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
		AssetFileDescriptor fd = activity.getContentResolver().openAssetFileDescriptor(uri, "r");
		FileInputStream fis = fd.createInputStream();
		return fis.toString();
//		CMFileManager.writeBufToFile("test.vcard", fis.toString().getBytes());
	}
}
